package com.szqhyxj.youxiangju.biz.partnerHome.equipment;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.biz.partnerHome.equipment.bean.BrandPOListItem;
import com.szqhyxj.youxiangju.biz.partnerHome.equipment.bean.StockBrand;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/10/30.
 */

public class PartnerEquipmentPresenter extends AbBasePresenter implements PartnerEquipmentContract.Presenter{

    private PartnerEquipmentContract.View view;

    private List<Disposable> disposables = new ArrayList<>();
    private StockBrand stockBrand;
    PartnerEquipmentPresenter(PartnerEquipmentContract.View view) {
        this.view = view;
    }

    @Override
    public void initStockBrand(){
       addDisposables(PartnerEquipmentModel.getStock()
               .observeOn(AndroidSchedulers.mainThread())
               .subscribeOn(Schedulers.io())
               .subscribe(new BaseConsumer<StockBrand>(){

                   @Override
                   protected void error(String code, String msg){
                       view.dismissLoading();
                   }
                   @Override
                   protected void successAccept(StockBrand stockBrand){
                       view.dismissLoading();
                       if(Integer.parseInt(stockBrand.getInventery())>0){
                           view.showStockBrand(stockBrand);
                           PartnerEquipmentPresenter.this.stockBrand = stockBrand;
                       }else {
                           view.showNone();
                       }
                   }

               }, new Consumer<Throwable>(){
                   @Override
                   public void accept(Throwable throwable) throws Exception{
                       view.dismissLoading();
                   }
               }));
    }

    @Override
    public void getStockList(String brandId){
        addDisposables(PartnerEquipmentModel.getStockList(brandId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<List<String>>(){
                    @Override
                    protected void error(String code, String msg){
                        view.dismissLoading();
                    }

                    @Override
                    protected void successAccept(List<String> strings){
                        view.dismissLoading();
                        view.showStockList(strings);
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                    }
                }));
    }

    @Override
    public void setBrandName(){
        if(stockBrand!=null){
            List<BrandPOListItem>  brandPOListItems = stockBrand.getBrandPOList();
            String[] brands = new String[brandPOListItems.size()];
            for(int i=0;i<brandPOListItems.size();i++){
                BrandPOListItem item  = brandPOListItems.get(i);
                brands[i] = item.getBrandName();
            }
            view.showPickerStock(brands);
        }
    }

    @Override
    public void onDestroy() {
        for (Disposable disposable : disposables) {
            if (disposable != null) {
                disposable.dispose();
            }
        }
    }


    @Override
    public void addDisposables(Disposable disposable){
        super.addDisposables(disposable);
        view.showLoading();
    }
}
